package com.xjtu.weixin.controller.config;

import com.xjtu.weixin.model.entity.MpAutoResponse;
import com.xjtu.weixin.model.entity.MpTextTemplate;
import com.xjtu.weixin.service.MpAutoResponseService;
import com.xjtu.weixin.service.MpTextTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author 杨凯
 * @create 2018-09-04 9:16
 **/
@Controller
@RequestMapping("/auto_response")
public class AutoResponseController {


	@Autowired
	private MpAutoResponseService autoResponseService;

	@Autowired
	private MpTextTemplateService templateService;

	/**
	 * 关键字列表
	 * @return
	 *
	 */
	@GetMapping
	public String keyWords(Model model){
		//TODO 未进行分页
		final List<MpAutoResponse> autoResponseList = autoResponseService.getKeywordsList();
		final List<MpTextTemplate> textTemplateList = templateService.getTextTemplateList();
		model.addAttribute("autoResponseList",autoResponseList);
		model.addAttribute("textTemplateList",textTemplateList);
		return "admin/config/keywords-list";
	}

	/**
	 * 添加关键字
	 * @param keywords 关键字
	 * @param keywordsType 关键字类型
	 * @param msgType 消息类型
	 * @param templateId 模板Id
	 * @param templateName 模板名称
	 * @return
	 */
	@PostMapping("/keyword/add")
	public ResponseEntity addKeywords(String keywords,String keywordsType,String msgType,String templateId,String templateName){
		MpAutoResponse mpAutoResponse = new MpAutoResponse();
		mpAutoResponse.setKeyword(keywords);
		mpAutoResponse.setMsgType(msgType);
		mpAutoResponse.setTemplateId(templateId);
		mpAutoResponse.setKeywordType(keywordsType);
		mpAutoResponse.setTemplateName(templateName);
		mpAutoResponse.setCreateBy("yang kai");
		mpAutoResponse.setCreateTime(new Date());
		autoResponseService.addKeywords(mpAutoResponse);
		return new ResponseEntity("添加成功！",HttpStatus.OK);
	}
}
